#include "pch.h"
#include "CButtonUI.h"


void CButtonUI::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	HDC hDC = lpDrawItemStruct->hDC;
	CRect rect = lpDrawItemStruct->rcItem;
	CDC dc;
	dc.Attach(hDC);//关联设备上下文
	CDC memDC;//创建内存dc
	memDC.CreateCompatibleDC(&dc);
	if (m_bPush && m_bHover)	//按下状态
	{
		memDC.SelectObject(m_hBitmapPush);
		dc.BitBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY);
	}
	else if (m_bHover)//悬停状态
	{
		memDC.SelectObject(m_hBitmapHover);
		dc.BitBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY);
		//瞄一下边框
		CPen pen;
		pen.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
		dc.SelectObject(&pen);
		dc.Draw3dRect(rect, 1, 1);
	}
	else
	{
		memDC.SelectObject(m_hBitmapNormal);
		dc.BitBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY);
	}

	dc.Detach();//取消关联设备上下文
}

CButtonUI::CButtonUI(LPCTSTR lpNormal, LPCTSTR lpHover, LPCTSTR lpPush)
{
	m_hBitmapNormal = (HBITMAP)LoadImage(AfxGetResourceHandle(), lpNormal, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);//加载正常状态图片
	m_hBitmapHover = (HBITMAP)LoadImage(AfxGetResourceHandle(), lpHover, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);//加载鼠标悬停状态图片
	m_hBitmapPush = (HBITMAP)LoadImage(AfxGetResourceHandle(), lpPush, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);//加载鼠标按下状态图片
	m_bPush = m_bHover = false;
}
BEGIN_MESSAGE_MAP(CButtonUI, CButton)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	//	ON_WM_NCMOUSELEAVE()
	ON_WM_MOUSELEAVE()
	//	ON_WM_MOUSEHOVER()
END_MESSAGE_MAP()


void CButtonUI::OnLButtonDown(UINT nFlags, CPoint point)
{
	m_bPush = true;

	CButton::OnLButtonDown(nFlags, point);
}


void CButtonUI::OnLButtonUp(UINT nFlags, CPoint point)
{
	m_bPush = false;

	CButton::OnLButtonUp(nFlags, point);
}


void CButtonUI::OnMouseMove(UINT nFlags, CPoint point)
{
	if (!m_bHover)
	{
		TRACKMOUSEEVENT tme;//鼠标移动事件
		tme.cbSize = sizeof(TRACKMOUSEEVENT);//结构体大小
		tme.dwFlags = TME_LEAVE;//鼠标离开
		tme.dwHoverTime = 10;//鼠标悬停时间
		tme.hwndTrack = m_hWnd;//窗口句柄
		_TrackMouseEvent(&tme);//跟踪鼠标事件
	}
	m_bHover = true;
	Invalidate(TRUE);

	CButton::OnMouseMove(nFlags, point);
}


//void CButtonUI::OnNcMouseLeave()
//{
	// 该功能要求使用 Windows 2000 或更高版本。
	// 符号 _WIN32_WINNT 和 WINVER 必须 >= 0x0500。
	// TODO: 在此添加消息处理程序代码和/或调用默认值

//	CButton::OnNcMouseLeave();
//}


void CButtonUI::OnMouseLeave()
{
	m_bHover = false;
	m_bPush = false;
	Invalidate(TRUE);
	CButton::OnMouseLeave();
}


//void CButtonUI::OnMouseHover(UINT nFlags, CPoint point)
//{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
//	m_bHover = true;
//	Invalidate(TRUE);
//	CButton::OnMouseHover(nFlags, point);
//}
